﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.WebControls;
using Eis51.CampanyCMS.Business.NewsManager;
using Eis51.CampanyCMS.Model.NewsManager;
using Eis51.CampanyCMS.Business;
using Eis51.CampanyCMS.Model.NewsManager.QueryO;
using Eis51.CampanyCMS.Model;
using Eis51.Web.Admin;

namespace Eis51.CampanyCMS.UI.Admin.NewsManager
{
    public partial class NewTypeTree : BasePage
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                //权限判断                  
                //if (!ChectRight(AdminType.Admin))
                //{
                //    GoNoRightPage();
                //}
                InitData();
            }
        }

        private void InitData()
        {
            RadTree1.Nodes.Clear();

            NewsTypeQuery query = new NewsTypeQuery();
            query.Licd = AppContext.LCID;
            query.CreateBy = AppContext.CurrentAdministrator.UserName;
            DataTable dt = NewstypeBLL.GetInstance().GetTypeByUsername(query);

            dt.DataSet.Relations.Add("NodeRelation", dt.DataSet.Tables[0].Columns["seqno"], dt.DataSet.Tables[0].Columns["ParentID"], false);

            foreach (DataRow dbRow in dt.DataSet.Tables[0].Rows)
            {

                if (dbRow["ParentID"].ToString() == "0")//这个就是第一层。
                {
                    RadTreeNode node = CreateNode(dbRow["title"].ToString(), false , dbRow["seqno"].ToString());
                    node.Category = "Folder";
                    RadTree1.AddNode(node);
                    RecursivelyPopulate(dbRow, node);
                }
            }

            dt.DataSet.Relations.Remove("NodeRelation");
        }



        private void RecursivelyPopulate(DataRow dbRow, RadTreeNode node)
        {
            foreach (DataRow childRow in dbRow.GetChildRows("NodeRelation"))
            {
                RadTreeNode childNode = CreateNode(childRow["title"].ToString(), true, childRow["seqno"].ToString());
                node.AddNode(childNode);
                RecursivelyPopulate(childRow, childNode);
            }
        }


        private RadTreeNode CreateNode(string text, bool expanded, string id)
        {
            RadTreeNode node = new RadTreeNode(text);
            node.Expanded = expanded;
            node.ID = id;
            return node;
        }



        protected void RadTree1_NodeEdit1(object o, RadTreeNodeEventArgs e)
        {
            RadTreeNode nodeEdited = e.NodeEdited;
            string newText = e.NewText;
            if (newText == null || newText == string.Empty)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "RadTree1_NodeEdit1", "<script>alert('命名不能为空！');</script>");
            }
            else
            {
                Newstype p = NewstypeBLL.GetInstance().Get(new Newstype.Key(int.Parse(nodeEdited.ID)));
                p.Title = txtNewstypename.Text;
                p.Showcontent = txtShowcontent.Text;
                p.Content = txtContent.Text;
                p.Createdate = DateTime.Now;
                p.Createby = AppContext.CurrentAdministrator.UserName;
                p.Code = txtCode.Text;
                p.Licd = AppContext.LCID;
                p.Lasteupdateby = AppContext.CurrentAdministrator.UserName;
                p.Lasteupdatedate = DateTime.Now;
                NewstypeBLL.GetInstance().Update(p);
                GenerateTreeView();
            }
        }

        protected void RadMenu1_ItemClick1(object sender, RadMenuEventArgs e)
        {
            HandleItemClicked(e);
        }

        private void HandleItemClicked(RadMenuEventArgs e)
        {
            switch (e.Item.Value)
            {
                case "Delete":
                    if (RadTree1.SelectedNode.Nodes.Count > 0)
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "Delete", "<script>alert('还存在有子节点，请先删除子节点！');</script>");
                    }
                    else
                    {
                        if (RadTree1.SelectedNode == RadTree1.Nodes[0]
                            && RadTree1.Nodes.Count == 1)
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "Delete1", "<script>alert('类型树必须要有一个以上节点，请不要删除！');</script>");
                        }
                        else
                        {

                            NewstypeBLL.GetInstance().Delete(NewstypeBLL.GetInstance().Get(new Newstype.Key(int.Parse(RadTree1.SelectedNode.ID))));
                            GenerateTreeView();
                        }
                    }
                    break;
                case "AddSubLevel":
                    RadTreeNode rt = new RadTreeNode("新类型");
                    Newstype p2 = new Newstype();
                    p2.Title = "新类型";
                    p2.Parentid = int.Parse(RadTree1.SelectedNode.ID);
                    p2.Createby = AppContext.CurrentAdministrator.UserName;
                    p2.Createdate = DateTime.Now;
                    p2.Lasteupdateby = AppContext.CurrentAdministrator.UserName;
                    p2.Createdate = DateTime.Now;
                    p2.Licd = AppContext.LCID;
                    NewstypeBLL.GetInstance().Insert(p2);
                    RadTree1.SelectedNode.AddNode(rt);
                    GenerateTreeView();
                    break;
                case "AddSameLevel":
                    RadTreeNode rt1 = new RadTreeNode("新类型");
                    Newstype p3= new Newstype();
                    p3.Title = "新类型";
                    p3.Parentid = RadTree1.SelectedNode.Parent == null ? 0 : int.Parse(RadTree1.SelectedNode.Parent.ID);
                    p3.Createby = AppContext.CurrentAdministrator.UserName;
                    p3.Createdate = DateTime.Now;
                    p3.Lasteupdateby = AppContext.CurrentAdministrator.UserName;
                    p3.Createdate = DateTime.Now;
                    p3.Licd = AppContext.LCID;
                    NewstypeBLL.GetInstance().Insert(p3);
                    RadTree1.SelectedNode.AddNode(rt1);
                    GenerateTreeView();
                    break;
            }
            ReNewCache();
        }

        private void GenerateTreeView()
        {
            RadTree1.Nodes.Clear();
            NewsTypeQuery query = new NewsTypeQuery();
            query.Licd = AppContext.LCID;
            query.CreateBy = AppContext.CurrentAdministrator.UserName;
            DataTable dt = NewstypeBLL.GetInstance().GetTypeByUsername(query);
 
            dt.DataSet.Relations.Add("NodeRelation", dt.DataSet.Tables[0].Columns["seqno"], dt.DataSet.Tables[0].Columns["ParentID"], false);

            foreach (DataRow dbRow in dt.DataSet.Tables[0].Rows)
            {

                if (dbRow["Parentid"].ToString() == "0")//这个就是第一层。
                {
                    RadTreeNode node = CreateNode(dbRow["title"].ToString(), false , dbRow["seqno"].ToString());
                    node.Category = "Folder";
                    RadTree1.AddNode(node);
                    RecursivelyPopulate(dbRow, node);
                }
            }
            dt.DataSet.Relations.Remove("NodeRelation");
        }

        private void ReNewCache()
        {

            // Dict.CacheData.ClearLocalCache(CacheName.AreaType.ToString());
        }




        protected void RadMenu2_ItemClick1(object sender, RadMenuEventArgs e)
        {
            HandleItemClicked(e);

        }

        protected void RadTree1_NodeClick(object o, RadTreeNodeEventArgs e)
        {
            SerId.Value = RadTree1.SelectedNode.ID;
            SetValue();

        }

        private void SetValue()
        {

            Newstype p = new Newstype();
            if (SerId.Value != string.Empty)
            {
                p = NewstypeBLL.GetInstance().Get(new Newstype.Key(int.Parse(SerId.Value)));
            }
          
            txtNewstypename.Text = p.Title;
            txtShowcontent.Text = p.Showcontent;
            txtContent.Text = p.Content;
            txtCode.Text = p.Code;
            this.btnSave.Enabled = true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (SerId.Value != string.Empty)
            {
                Newstype p = NewstypeBLL.GetInstance().Get(new Newstype.Key(int.Parse(SerId.Value)));
                p.Title = txtNewstypename.Text;
                p.Showcontent = txtShowcontent.Text;
                p.Code = txtCode.Text;
                p.Content = txtContent.Text;
                NewstypeBLL.GetInstance().Update(p);
                ClaerFace();
                InitData();
            }
        }

        private void ClaerFace()
        {
            SerId.Value = "";
            txtNewstypename.Text = "";
            txtShowcontent.Text = "";
            txtContent.Text = "";
            txtContent.Text = "";
        }

        protected void bntSearch_Click(object sender, EventArgs e)
        {

            InitData();
        }

    }
}
